/**
 * 
 */
package com.oscarmovie.action;

import java.util.Map;

import weibo4j.model.User;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.oscarmovie.model.Movie;
import com.oscarmovie.model.Person;
import com.oscarmovie.model.Statistics;
import com.oscarmovie.service.MovieService;
import com.oscarmovie.service.PersonService;
import com.oscarmovie.service.StatisticsService;

/**
 * @author zhengda
 *
 */
@SuppressWarnings("serial")
public class StatisticsAction extends ActionSupport {
	
	private MovieService movieService;
	private PersonService personService;
	private StatisticsService statisticsService;
	
	private int movieId;
	private int personId;
	private float score;
	private Statistics statistics;
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateMovieScore() throws Exception{
		Movie movie = movieService.load(movieId);
		statistics = movie.getStatistics();
		updateScore(statistics);
		Map<String, Object> session = ActionContext.getContext().getSession();
		User user = session.get("user")==null?null:(User)session.get("user");
		if(user!=null){
			
		}
		return Action.SUCCESS;
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updatePersonScore() throws Exception{
		Person pserson = personService.load(personId);
		statistics = pserson.getStatistics();
		updateScore(statistics);
		
		return Action.SUCCESS;
	}
		
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateWanted() throws Exception{
		Movie movie = movieService.load(movieId);
		statistics = movie.getStatistics();
		statistics.setWanted(statistics.getWanted() + 1);
		statisticsService.save(statistics);
				
		return Action.SUCCESS;
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateWatched() throws Exception{
		Movie movie = movieService.load(movieId);
		statistics = movie.getStatistics();
		statistics.setWatched(statistics.getWatched() + 1);
		statisticsService.save(statistics);
		
		return Action.SUCCESS;
	}
	
	public String updateClicked() throws Exception{
		Movie movie = movieService.load(movieId);
		statistics = movie.getStatistics();
		statistics.setClicked(statistics.getClicked() + 1);
		statisticsService.save(statistics);
		return Action.SUCCESS;
	}
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public String updateFavorite() throws Exception{
		Person person = personService.load(personId);
		statistics = person.getStatistics();
		statistics.setFavorite(statistics.getFavorite() + 1);
		statisticsService.save(statistics);
		
		return Action.SUCCESS;
	}
	
	/**
	 * 
	 * @param statistics
	 */
	private void updateScore(Statistics statistics) {
		score = (statistics.getScore() * statistics.getScored() + score)/(statistics.getScored()+1);
		statistics.setScore(score);
		statistics.setScored(statistics.getScored()+1);
		statisticsService.save(statistics);
	}

	/**
	 * @return the score
	 */
	public float getScore() {
		return score;
	}

	/**
	 * @param score the score to set
	 */
	public void setScore(float score) {
		this.score = score;
	}

	/**
	 * @return the statistics
	 */
	public Statistics getStatistics() {
		return statistics;
	}

	/**
	 * @param movieService the movieService to set
	 */
	public void setMovieService(MovieService movieService) {
		this.movieService = movieService;
	}

	/**
	 * @param personService the personService to set
	 */
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	/**
	 * @param statisticsService the statisticsService to set
	 */
	public void setStatisticsService(StatisticsService statisticsService) {
		this.statisticsService = statisticsService;
	}

	/**
	 * @param movieId the movieId to set
	 */
	public void setMovieId(int movieId) {
		this.movieId = movieId;
	}

	/**
	 * @param personId the personId to set
	 */
	public void setPersonId(int personId) {
		this.personId = personId;
	}
	
	
	
}
